package org.imlinker.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.imlinker.LocalVariablesManager;
import org.imlinker.account.ManageAccount;
import org.imlinker.list.ViewList;
import org.imlinker.login.Account;
import org.imlinker.setting.Settings;

/**
 * The window of settings.
 *  
 */

/**
 * Here we have added language selection but there are still many problems to
 * handle. now we save your preference on you local disk later we will use
 * Preference APIs to fulfill this function
 * 
 */
public final class SettingsWindow extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	private static SettingsWindow settingsWindow = null;

	/**
	 * Default textField length.
	 */

	private final int textFieldLength = 15;

	/**
	 * 
	 */
	private JTextField localnameField = new JTextField(textFieldLength);

	/**
	 * 
	 */
	private JCheckBox openFirst = new JCheckBox(LocalVariablesManager
			.getLocalVariables("SettingsWindow.AutoPopupMessageCheckBox"));

	/**
	 * 
	 */
	private JComboBox languageComboBox = new JComboBox();

	/**
	 * 
	 */
	private JButton saveButton = new JButton(LocalVariablesManager
			.getLocalVariables("SettingsWindow.SaveButton"));

	/**
	 * 
	 * 
	 */
	private SettingsWindow() {
		super(LocalVariablesManager.getLocalVariables("SettingsWindow"));
		setName("SettingsWindow");
		setIconImage(ManageAccount.getLogo());

		String localName = Settings.getSettings().getValue("localname");
		if (localName != null) {
			localnameField.setText(localName);
		}

		String openFirstStr = Settings.getSettings().getValue("openfirst");
		if (openFirstStr != null) {
			openFirst.setSelected(Boolean.valueOf(openFirstStr));
		}

		String language = LocalVariablesManager.getSettings("Language");

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0, 2));

		// Local Name.
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("SettingsWindow.LocalNameLabel")));
		panel.add(localnameField);

		// Open first.
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("SettingsWindow.AutoPopupMessageLabel")));
		panel.add(openFirst);

		// Language selection.
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("SettingsWindow.LanguageSelectionLabel")));
		File languageDir = new File(ManageAccount.getConfigPath());
		if (languageDir.exists()) {
			File[] files = languageDir.listFiles();
			for (int t = 0; t < files.length; t++) {
				if (files[t].getName().endsWith(".lng")) {
					languageComboBox.addItem(files[t].getName().substring(0,
							files[t].getName().lastIndexOf(".")));
				}
			}
		}
		languageComboBox.setSelectedItem(language);
		panel.add(languageComboBox);

		// Remind Sound ,and close or not.
		// TODO translate label.
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("SettingsWindow.SoundNotifyLabel")));
		final JCheckBox notifyBox = new JCheckBox(LocalVariablesManager
				.getLocalVariables("SettingsWindow.SoundNotifyCheckBox"), true);
		final JTextField path = new JTextField();
		final JButton selectPath = new JButton(LocalVariablesManager
				.getLocalVariables("SettingsWindow.SoundPathButton"));
		selectPath.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				int returnVal = fileChooser.showOpenDialog(SettingsWindow.this);

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fileChooser.getSelectedFile();

					path.setText(file.getAbsolutePath());
				}
			}

		});

		// Set the current setting.
		String soundNotify = Settings.getSettings().getValue("soundnotify");
		if (soundNotify != null) {
			boolean b = Boolean.valueOf(soundNotify);
			notifyBox.setSelected(b);
			path.setEnabled(b);
			selectPath.setEnabled(b);
		}
		String soundPath = Settings.getSettings().getValue("soundfilepath");
		if (soundPath != null) {
			path.setText(soundPath);
		}
		// Add change Listener .
		notifyBox.addChangeListener(new ChangeListener() {
			public void stateChanged(final ChangeEvent e) {
				if (notifyBox.isSelected()) {
					path.setEnabled(true);
					selectPath.setEnabled(true);
				} else {
					path.setEnabled(false);
					selectPath.setEnabled(false);
				}
			}
		});

		// Add to panel.
		panel.add(notifyBox);
		panel.add(new JLabel(LocalVariablesManager
				.getLocalVariables("SettingsWindow.SoundPathLabel")));
		panel.add(selectPath);
		panel.add(new JLabel(""));
		panel.add(path);

		// Save Button.
		panel.add(new JLabel(""));
		saveButton.addActionListener(new ActionListener() {

			public void actionPerformed(final ActionEvent e) {
				Settings.getSettings().setValue("localname",
						localnameField.getText());
				Settings.getSettings().setValue("openfirst",
						Boolean.toString(openFirst.isSelected()));
				Settings.getSettings().setValue("soundnotify",
						Boolean.toString(notifyBox.isSelected()));
				Settings.getSettings()
						.setValue("soundfilepath", path.getText());
				if (languageComboBox.getSelectedIndex() == -1) {
					LocalVariablesManager.setSettings("Language",
							((JTextField) languageComboBox.getEditor()
									.getEditorComponent()).getText());
				} else {
					LocalVariablesManager.setSettings("Language",
							languageComboBox.getSelectedItem().toString());
				}

				Account.setLocalName(Settings.getSettings().getValue(
						"localname"));
				TrayIconListener.setOpenFirst(Boolean.valueOf(Settings
						.getSettings().getValue("openfirst")));
				ViewList.getBuddyList().setSoundNotify(
						Boolean.valueOf(Settings.getSettings().getValue(
								"soundnotify")));
				SettingsWindow.this.dispose();
			}

		});
		panel.add(saveButton);

		getContentPane().add(panel, BorderLayout.NORTH);

		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * 
	 * @return The SettingWindow.
	 */
	public static SettingsWindow getSettingWindow() {
		if (settingsWindow == null) {
			settingsWindow = new SettingsWindow();
		}

		return settingsWindow;
	}
}
